package com.game.framework.message.stream;

import com.alibaba.fastjson.JSON;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

public class ServiceMessageHeader extends AbstractMessageHeader {
    private int serviceId;
    private int fromGameServerId;
    private int toGameServerId;
    private long userId;
    private int errorCode;
	// 消息类型
	private EnumMessageType messageType;
    // 留下一个扩展的对象，添加新的字段信息时，就不需要修改协议序列化了。
    private HeaderAttribute attr = new HeaderAttribute();
    public ServiceMessageHeader() {
        super();
        int size = 22;// short int,int  long的长度,还有4字节是attr数据的长度
        this.addHeaderLength(size);
    }

    
    public EnumMessageType getMessageType() {
		return messageType;
	}


	public void setMessageType(EnumMessageType messageType) {
		this.messageType = messageType;
	}


	public int getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(int errorCode) {
		this.errorCode = errorCode;
	}


	public int getServiceId() {
		return serviceId;
	}


	public void setServiceId(int serviceId) {
		this.serviceId = serviceId;
	}


	@Override
    public void read(ByteBuf byteBuf) {
        super.read(byteBuf);
        this.errorCode = byteBuf.readInt();
        this.serviceId = byteBuf.readShort();
        this.fromGameServerId = byteBuf.readInt();
        this.toGameServerId = byteBuf.readInt();
        this.messageType = EnumMessageType.getType(byteBuf.readByte());
        this.userId = byteBuf.readLong();
        int attrLength = byteBuf.readInt();
        // 读取attr对象的json串信息
        String json = byteBuf.readCharSequence(attrLength, CharsetUtil.UTF_8).toString();
        this.attr = JSON.parseObject(json, HeaderAttribute.class);
    }

    private ByteBuf serializedAttr() {
        if (attr == null) {
            attr = new HeaderAttribute();
        }
        String json = JSON.toJSONString(attr);
        byte[] bytes = json.getBytes(CharsetUtil.UTF_8);
        return Unpooled.wrappedBuffer(bytes);
    }

    @Override
    public ByteBuf write() {
        ByteBuf byteBuf =  super.write();
        byteBuf.writeInt(this.errorCode);
        byteBuf.writeShort(this.serviceId);
        byteBuf.writeInt(this.fromGameServerId);
        byteBuf.writeInt(this.toGameServerId);
        byteBuf.writeByte(this.messageType.getType());
        byteBuf.writeLong(userId);
        ByteBuf attrBuf = this.serializedAttr();
        byteBuf.writeInt(attrBuf.readableBytes());
        CompositeByteBuf result = Unpooled.compositeBuffer(2);
        result.addComponent(true,byteBuf);
        result.addComponent(true,attrBuf);
        return result;
    }

    public HeaderAttribute getAttr() {
        return attr;
    }

    public void setAttr(HeaderAttribute attr) {
        this.attr = attr;
    }

    public int getFromGameServerId() {
        return fromGameServerId;
    }

    public void setFromGameServerId(int fromServerId) {
        this.fromGameServerId = fromServerId;
    }

    public int getToGameServerId() {
        return toGameServerId;
    }

    public void setToGameServerId(int toServerId) {
        this.toGameServerId = toServerId;
    }

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }


	@Override
	public String toString() {
		return "ServiceMessageHeader [getErrorCode()=" + getErrorCode() + ", getServiceId()=" + getServiceId()
				+ ", getFromServerId()=" + getFromGameServerId() + ", getToServerId()=" + getToGameServerId()
				+ ", getUserId()=" + getUserId() + ", getMessageId()=" + getMessageId() + ", getMessageSeqId()="
				+ getMessageSeqId() + ", getVersion()=" + getVersion() + "]";
	}
    
    
}
